/*
 * Copyright 2016 jeasonlzy(廖子尧)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aierjun.ani.net.OkGo.download;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.aierjun.ani.R;
import com.aierjun.ani.init.GlideUtils;
import com.aierjun.ani.net.OkGo.NumberProgressBar;
import com.aierjun.ani.net.OkGo.okupload.UploadBtnClickListenter;
import com.aierjun.ani.net.OkGo.okupload.UploadEntity;

import java.util.List;


public class DownloadOverAdapter extends BaseAdapter{
    private Context context;
    private List<DownLoadEntity> list;
    private UploadBtnClickListenter onClickListener;

    public DownloadOverAdapter(Context context, UploadBtnClickListenter onClickListener ) {
        this.context = context;
        this.onClickListener = onClickListener;
    }

    public void setDatas(List<DownLoadEntity> list){
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_upload_over,null);
            viewHolder.img = (ImageView) convertView.findViewById(R.id.icon);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.progress = (TextView) convertView.findViewById(R.id.netSpeed);
            viewHolder.progressBar = (NumberProgressBar) convertView.findViewById(R.id.pbProgress);
            viewHolder.remove = (Button) convertView.findViewById(R.id.remove);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        UploadEntity uploadEntity = (UploadEntity) getItem(position);
        GlideUtils.load(context,uploadEntity.getFile(),viewHolder.img);
        viewHolder.name.setText(uploadEntity.getName());
        if (uploadEntity.getStatus() == 5){  //这里只会去修改本地读取的数据的状态，新加的数据状态不受影响
            viewHolder.progress.setText("上传完成");
            viewHolder.progressBar.setProgress(100);
        } else if (uploadEntity.getStatus() == 2){
            viewHolder.progress.setText("上传失败");
            viewHolder.progressBar.setProgress(50);
        }
        viewHolder.remove.setTag(uploadEntity.getTag());
        viewHolder.remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onClick(v, (String) v.getTag(),"REMOVE");
            }
        });

        return convertView;
    }

    class ViewHolder{
        ImageView img;
        TextView name;
        TextView progress;
        NumberProgressBar progressBar;
        Button remove;
    }

    public  List<DownLoadEntity> removeData(String tag){
        DownLoadEntity uploadEntity;
        for (int i = 0;i<list.size();i++){
            uploadEntity = list.get(i);
            if (uploadEntity.getTag().equals(tag))
                list.remove(i);
        }
        notifyDataSetChanged();
        return list;
    }

    public List<DownLoadEntity> addData(DownLoadEntity entity){
        list.add(entity);
        notifyDataSetChanged();
        return list;
    }
}
